package com.heima.health.controller;

import com.heima.health.dto.SetmealDto;
import com.heima.health.entity.QueryPageBean;
import com.heima.health.entity.Result;
import com.heima.health.service.SetmealService;
import com.heima.health.utils.aliyunoss.AliyunUtils;
import com.heima.health.utils.redis.RedisUtil;
import com.heima.health.utils.resources.RedisConstant;
import com.heima.health.utils.resources.UploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("setmeal")
@Api(tags = "检查组管理开发")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @PostMapping("findPage")
    @ApiOperation(value = "分页查询",notes = "分页查询所有套餐列表信息")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        return new Result(setmealService.findPage(queryPageBean));
    }

    @PostMapping("saveOrUpdate")
    @ApiOperation(value = "新增或更新检查组",notes = "未携带id时新增，携带id时更新检查组")
    public Result saveOrUpdate(@RequestBody SetmealDto setmealDto){
        return new Result(setmealService.saveUpdate(setmealDto));
    }

    @GetMapping("findGidBySid/{sid}")
    @ApiOperation(value = "根据套餐id查询检查组id",notes = "根据传过来的id查询检查组ids")
    public Result findGidBySid(@PathVariable("sid") Integer sid){
        return new Result(setmealService.findBySid(sid));
    }

//    @DeleteMapping("deleteById/{id}")
//    @ApiOperation(value = "逻辑删除",notes = "根据id删除检查组")
//    public Result deleteById(@PathVariable("id") Integer id){
//        CheckGroup checkGroup=new CheckGroup();
//        checkGroup.setId(id);
//        checkGroup.setIs_delete(1);
//        return new Result(checkGroupService.updateById(checkGroup));
//    }

    @PostMapping("upload")
    @ApiOperation(value = "图片上传",notes = "将图片上传到阿里云并返回图片id")
    public Result upload(@RequestParam("imgFile") MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        String uuidname = UploadUtils.generateRandonFileName(filename);
        AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(), uuidname);
        //  图片存储 集合 + 单独保存一个过期时间
        RedisUtil.addToSet(RedisConstant.ALL_SETMEAL_PIC_SET,uuidname);
        RedisUtil.set(RedisConstant.SINGLE_PIC+uuidname,uuidname,60, TimeUnit.SECONDS);
        return new Result(uuidname);
    }
}
